Skip to content

Fix combinations() and combinations_with_replacement() checking r before the iterable - #1357

Open
jseop-lim wants to merge 1 commit into
oracle:masterfrom
jseop-lim:fix/itertools-combinations-iterable-first
Open

jseop-lim wants to merge 1 commit into
oracle:masterfrom
jseop-lim:fix/itertools-combinations-iterable-first

Conversation

@jseop-lim

Copy link
Copy Markdown
Contributor

Description

Part of #1291 (item 12).

Both constructors check r < 0 before they read the iterable (CombinationsBuiltins.java#L134-L139, CombinationsWithReplacementBuiltins.java#L109-L113). CPython builds the pool first (itertools_combinations_impl, itertools_combinations_with_replacement_impl).

AS-IS

>>> itertools.combinations(1, -1)
ValueError: r must be non-negative
>>> itertools.combinations_with_replacement(1, -1)
ValueError: r must be non-negative

TO-BE

>>> itertools.combinations(1, -1)
TypeError: 'int' object is not iterable
>>> itertools.combinations_with_replacement(1, -1)
TypeError: 'int' object is not iterable

(matching CPython)

Changes

  • Read the iterable before the r check in both constructors, as PermutationsBuiltins does.
  • Add a test suite to tests/test_itertools.py.

Testing

mx python-jvm on darwin-aarch64, then mx graalpytest on test_itertools.py (the new test fails on the unpatched build and passes with the fix) and the tagged test_itertools (124 passed). mx python-checkcopyrights, mx eclipseformat and mx checkstyle are clean.

combinations(1, -1) and combinations_with_replacement(1, -1) raised
ValueError for r where CPython first fails to iterate over the first
argument with TypeError.

Signed-off-by: Jeongseop Lim <jeongseop_lim@korea.ac.kr>
@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Nov 25, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant